<template>
  <el-dialog
     v-drag
    :title="!dataForm.factoryId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="厂家编号" prop="factoryCode">
      <el-input size="mini"  v-model="dataForm.factoryCode" placeholder="厂家编号" clearable></el-input>
    </el-form-item>
    <el-form-item label="厂家名称" prop="factoryzName">
      <el-input size="mini"  v-model="dataForm.factoryzName" placeholder="厂家名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="营业执照号码" prop="factoryNo">
      <el-input size="mini"  v-model="dataForm.factoryNo" placeholder="营业执照号码" clearable></el-input>
    </el-form-item>
    <el-form-item label="所在省份" prop="provinceName">
      <el-input size="mini"  v-model="dataForm.provinceName" placeholder="所在省份" clearable></el-input>
    </el-form-item>
    <el-form-item label="所在省份编码" prop="provinceCode">
      <el-input size="mini"  v-model="dataForm.provinceCode" placeholder="所在省份编码" clearable></el-input>
    </el-form-item>
    <el-form-item label="城市名称" prop="cityCame">
      <el-input size="mini"  v-model="dataForm.cityCame" placeholder="城市名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="城市编号" prop="cityCode">
      <el-input size="mini"  v-model="dataForm.cityCode" placeholder="城市编号" clearable></el-input>
    </el-form-item>
    <el-form-item label="所在区域ID" prop="zoneCode">
      <el-input size="mini"  v-model="dataForm.zoneCode" placeholder="所在区域ID" clearable></el-input>
    </el-form-item>
    <el-form-item label="区域名称" prop="zoneName">
      <el-input size="mini"  v-model="dataForm.zoneName" placeholder="区域名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="企业地址" prop="address">
      <el-input size="mini"  v-model="dataForm.address" placeholder="企业地址" clearable></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input size="mini"  v-model="dataForm.email" placeholder="邮箱" clearable></el-input>
    </el-form-item>
    <el-form-item label="传真" prop="fax">
      <el-input size="mini"  v-model="dataForm.fax" placeholder="传真" clearable></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contactor">
      <el-input size="mini"  v-model="dataForm.contactor" placeholder="联系人" clearable></el-input>
    </el-form-item>
    <el-form-item label="联系人电话" prop="mobile">
      <el-input size="mini"  v-model="dataForm.mobile" placeholder="联系人电话" clearable></el-input>
    </el-form-item>
    <el-form-item label="企业网站" prop="website">
      <el-input size="mini"  v-model="dataForm.website" placeholder="企业网站" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input size="mini"  v-model="dataForm.createTime" placeholder="创建时间" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input size="mini"  v-model="dataForm.updateTime" placeholder="修改时间" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUsername">
      <el-input size="mini"  v-model="dataForm.createUsername" placeholder="创建人" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createUserId">
      <el-input size="mini"  v-model="dataForm.createUserId" placeholder="创建人id" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateUsername">
      <el-input size="mini"  v-model="dataForm.updateUsername" placeholder="修改人" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改人id" prop="updateUserId">
      <el-input size="mini"  v-model="dataForm.updateUserId" placeholder="修改人id" clearable></el-input>
    </el-form-item>
    <el-form-item label="是否可用 1:是" prop="status">
      <el-input size="mini"  v-model="dataForm.status" placeholder="是否可用 1:是" clearable></el-input>
    </el-form-item>
    <el-form-item label="是否删除 1:是" prop="isDeleted">
      <el-input size="mini"  v-model="dataForm.isDeleted" placeholder="是否删除 1:是" clearable></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input size="mini"  v-model="dataForm.remark" placeholder="备注" clearable></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input size="mini"  v-model="dataForm.version" placeholder="版本号" clearable></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          factoryId: 0,
          factoryCode: '',
          factoryzName: '',
          factoryNo: '',
          provinceName: '',
          provinceCode: '',
          cityCame: '',
          cityCode: '',
          zoneCode: '',
          zoneName: '',
          address: '',
          email: '',
          fax: '',
          contactor: '',
          mobile: '',
          website: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          isDeleted: '',
          remark: '',
          version: ''
        },
        dataRule: {
          factoryCode: [
            { required: true, message: '厂家编号不能为空', trigger: 'blur' }
          ],
          factoryzName: [
            { required: true, message: '厂家名称不能为空', trigger: 'blur' }
          ],
          factoryNo: [
            { required: true, message: '营业执照号码不能为空', trigger: 'blur' }
          ],
          provinceName: [
            { required: true, message: '所在省份不能为空', trigger: 'blur' }
          ],
          provinceCode: [
            { required: true, message: '所在省份编码不能为空', trigger: 'blur' }
          ],
          cityCame: [
            { required: true, message: '城市名称不能为空', trigger: 'blur' }
          ],
          cityCode: [
            { required: true, message: '城市编号不能为空', trigger: 'blur' }
          ],
          zoneCode: [
            { required: true, message: '所在区域ID不能为空', trigger: 'blur' }
          ],
          zoneName: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '企业地址不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '传真不能为空', trigger: 'blur' }
          ],
          contactor: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '联系人电话不能为空', trigger: 'blur' }
          ],
          website: [
            { required: true, message: '企业网站不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.factoryId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.factoryId) {
            const res = await this.$http({
              url: `/product/factory/info/${this.dataForm.factoryId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.baseFactory
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/product/factory/${!this.dataForm.factoryId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
